﻿using Agile.Infra.Core.Exceptions;
using Agile.Shared.WebApi.Extensions;

namespace Agile.Shared.WebApi.Controllers;

public class AgileControllerBase : ControllerBase
{
    [NonAction]
    protected virtual ResponseResult<T> Result<T>(T @this, string message = "success", bool success = true, ResponseStatus status = ResponseStatus.Code200)
    {
        if (@this is bool value)
        {
            if (!value)
            {
                throw new BusinessException("保存失败");
            }
        }

        return new ResponseResult<T>() { Status = status, Result = @this, Message = message, Success = success };
    }

    [NonAction]
    protected virtual ResponseResult<T> Fail<T>(string message = "fail")
    {
        return new ResponseResult<T>() { Status = ResponseStatus.Code200, Message = message, Success = false };
    }

    [NonAction]
    protected UserContext LoginUser()
    {
        return this.GetService<UserContext>();
    }
}
